(() => {
    let ele = {};

    ele.options = $('#tab-team li');
    ele.options2 = $('#tab-two button');

    ele.direct = $('#direct')//直推
    ele.indirect = $('#indirect')//间推

    ele.familLevel = $('#familLevel')//家族等级图标
    ele.famillevelIMG = $('#famillevelIMG')//家族等级
    ele.Directpush = $('#Directpush')//直推仙王
    ele.Familypush = $('#Familypush')//家族仙王

    ele.directNum = $('#directNum')//直推人数
    ele.indirectNum = $('#indirectNum')//间推人数
    ele.listEmpty = $('#list-empty');
    ele.listEmpty2 = $('#list-empty2');
    ele.mores = $('.more');

    ele.upLevel = $('#upLevel')//晋升
    // 恭喜您晋升弹框
    ele.modal_promotion = $('#modal-promotion')
    ele.confirm_promotion = $('#confirm-promotion')
    ele.familyimg = $('#familyimg')
    ele.flevel = $('#flevel')

    ele.confirm_promotion.onclick = () => {
        ele.modal_promotion.style.display = 'none'
    }
    const upData = [
        {
            level: '1-2',
            zhi: 5,
            num: 10,
            active: 100,
        },
        {
            level: '2-3',
            zhi: 10,
            num: 30,
            active: 500,
        },
        {
            level: '3-4',
            zhi: 15,
            num: 50,
            active: 1000,
        },
        {
            level: '4-5',
            zhi: 25,
            num: 100,
            active: 2000,
        },
        {
            level: '5-6',
            zhi: 50,
            num: 200,
            active: 5000,
        },
        {
            level: '6-7',
            zhi: 50,
            num: 300,
            active: 10000,
        },
        {
            level: '7-8',
            zhi: 100,
            num: 500,
            active: 20000,
        },
        {
            level: '8-9',
            zhi: 100,
            num: 1000,
            active: 50000,
        },
        {
            level: '9-10',
            zhi: 200,
            num: 2000,
            active: 100000,
        },
        {
            level: '至尊',
            zhi: 500,
            num: 5000,
            active: 300000,
        },
    ]
    const Effective = [
        {
            name: '直推',
            name2: '名仙王',
            num: 0,
            is_receive: 0
        },
        {
            name: '家族总共拥有',
            name2: '名仙王',
            num: 0,
            is_receive: 0
        }
    ]

    // 不可晋升弹框
    ele.modal_noUp = $('#modal-noUp')
    ele.noUp_close = $('#noUp-close')
    ele.noUpList = $('#noUpList')

    ele.noUp_close.onclick = () => {
        ele.modal_noUp.style.display = 'none'
    }
    ele.upLevel.onclick = () => {
        getList2();
    }
    // 不可晋升列表
    function getList2() {
        let token = getCookie("token");
        let userid = getCookie("userid");
        ajax({
            url: API_PATH.PROMOTECOLLECT,
            method: 'POST',
            header: {
                Authorization: token
            },
            success: res => {
                if (res.code == 1) {
                    if (res.data.complete == 1) {
                        ele.direct.innerHTML = ''
                        ele.indirect.innerHTML = ''
                        ele.familyimg.src = 'http://img.qunx.top/xianwang/family/' + res.data.family_level + '.png'
                        if (res.data.family_level == 11) {
                            ele.flevel.innerHTML = '至尊级家族'
                        } else {
                            ele.flevel.innerHTML = res.data.family_level + '级家族'
                        }
                        ele.modal_promotion.style.display = 'block'
                        getFriendList(typeList.indexOf(typeName), currentPage[typeName]);
                    } else {
                        for (let i = 0; i < Effective.length; i++) {
                            Effective[0].num = upData[getCookie('family_level') - 1].zhi
                            Effective[1].num = upData[getCookie('family_level') - 1].num

                            Effective[0].is_receive = getCookie('invitation_count') >= upData[getCookie('family_level') - 1].zhi ? 1 : 0
                            Effective[1].is_receive = getCookie('invitation_total') >= upData[getCookie('family_level') - 1].num ? 1 : 0
                        }
                        drawPropList2(Effective)
                    }

                } else {
                    openTips(res.msg)
                }

            }
        });
    }
    // 渲染不可晋升列表
    function drawPropList2(data) {
        console.log(data);
        let html = '';
        for (let i = 0; i < data.length; i++) {
            let lv = data[i];
            let status = lv.is_receive ? 'received' : 'receive';
            let txt = lv.is_receive == 1 ? '已完成' : '去完成';

            let go = "location.href='inviteFriends.html'"

            let p = ''
            if (i == 2) {
                p = '<p class="n1">' + lv.name + lv.num + '</p>'
            } else {
                p = '<p class="n1">' + lv.name + lv.num + lv.name2 + '</p>'
            }
            html += '<div class="nobpx">' +
                '<div class="noleft">' +
                p +
                '</div>' +
                '<button class="' + status + '" onclick=' + go + ' >' +
                '<span class="salen">' + txt + '</span>' +
                '</button>' +
                '</div>'
        }
        ele.noUpList.innerHTML = html;
        ele.modal_noUp.style.display = 'block'

    }







    // 玩家信息弹框
    ele.modal_message = $('#modal-message')
    ele.close_message = $('#close-message')
    ele.header = $('#header')//弹框头像
    ele.nickname = $('#nickname')//弹框昵称
    ele.vip = $('#vip')//弹框VIP显示
    ele.familyLevel = $('#familyLevel')//弹框家族等级
    ele.myinfo = $('#myinfo')//弹框区服
    ele.ID = $('#ID')//弹框ID
    ele.IDS = $('.IDS')//弹框复制信息
    ele.weixin = $('#weixin')//弹框微信
    ele.QQ = $('#QQ')//弹框QQ
    ele.copy = $('.copy')////弹框复制

    ele.close_message.onclick = () => {
        ele.modal_message.style.display = 'none'
    }


    const typeList = ['', 'one', 'two', 'three', 'four'];
    let typeName = 'one';
    let currentPage = {
        one: 1,
        two: 2,
        three: 3,
        four: 4
    }
    getFriendList(typeList.indexOf(typeName), currentPage[typeName]);
    getFriendList2(typeList.indexOf(typeName), currentPage[typeName]);

    tabBar();
    // 选项卡选项功能
    function tabBar() {
        for (let i = 0; i < ele.options.length; i++) {
            ele.options[i].addEventListener('click', e => {
                let cls = e.target.parentElement.className;
                typeName = cls.split(' ')[0].split('-')[1];
                let typeIndex = typeList.indexOf(typeName);

                let more = ele.mores[0];
                let more2 = ele.mores[1];

                if (currentPage[typeName] == 1) {
                    ele.direct.innerHTML = ''
                    ele.indirect.innerHTML = ''

                    more.style.display = "block";
                    more2.style.display = "block";

                    getFriendList(typeIndex, currentPage[typeName]);
                    getFriendList2(typeIndex, currentPage[typeName]);
                } else if (currentPage[typeName] == 2) {
                    // 有效好友
                    currentPage[typeName] = 1
                    ele.direct.innerHTML = ''
                    ele.indirect.innerHTML = ''

                    more.style.display = "block";
                    more2.style.display = "block";
                    
                    getFriendList(typeIndex, currentPage[typeName]);
                    getFriendList2(typeIndex, currentPage[typeName]);
                } else if (currentPage[typeName] == 3) {
                    // 仙王好友
                    currentPage[typeName] = 1
                    ele.direct.innerHTML = ''
                    ele.indirect.innerHTML = ''

                    more.style.display = "block";
                    more2.style.display = "block";

                    getFriendList(typeIndex, currentPage[typeName]);
                    getFriendList2(typeIndex, currentPage[typeName]);
                } else if (currentPage[typeName] == 4) {
                    // VIP好友
                    currentPage[typeName] = 1
                    ele.direct.innerHTML = ''
                    ele.indirect.innerHTML = ''

                    more.style.display = "block";
                    more2.style.display = "block";

                    getFriendList(typeIndex, currentPage[typeName]);
                    getFriendList2(typeIndex, currentPage[typeName]);
                }


            })
        }
    }

    // tabBar2()
    // function tabBar2() {
    //     for (let i = 0; i < ele.options2.length; i++) {
    //         ele.options2[i].addEventListener('click', e => {
    //             let cls = e.target.className;
    //             typeName = cls.split(' ')[0].split('-')[1];
    //             if (Name == 'one') {
    //                 console.log(Name, typeName);
    //             } else if (Name == 'two') {
    //                 console.log(Name, typeName);
    //             } else if (Name == 'three') {
    //                 console.log(Name, typeName);
    //             } else if (Name == 'four') {
    //                 console.log(Name, typeName);
    //             }
    //         })
    //     }
    // }

    // 拉取直推列表
    function getFriendList(type, page) {
        // console.log(type, page);
        let more = ele.mores[0];
        more.innerText = '加载中...';
        let token = getCookie("token");
        let userid = getCookie("userid");
        ajax({
            url: API_PATH.FAMILYLIST,
            method: 'GET',
            header: {
                Authorization: token
            },
            data: {
                page: page,
                invite_type: 1,
                type: type
            },
            success: res => {
                if (res.code == 1) {
                    ele.directNum.innerHTML = '直推(' + res.data.list.total + ')';
                    let detail = res.data.list.data
                    // console.log(res.data);
                    ele.familLevel.src = 'http://img.qunx.top/xianwang/family/' + res.data.family_level + '.png'//家族等级图标
                    ele.famillevelIMG.innerHTML = res.data.family_level + '级家族'//家族等级
                    ele.Directpush.innerHTML = res.data.invitation_count + '/' + upData[res.data.family_level - 1].zhi//直推仙王
                    setCookie('invitation_count', res.data.invitation_count)//直推仙王
                    setCookie('invitation_total', res.data.invitation_total)//家族仙王
                    ele.Familypush.innerHTML = res.data.invitation_total + '/' + upData[res.data.family_level - 1].num//家族仙王
                    if (detail.length == 0) {
                        more.style.display = "none";
                        ele.listEmpty.style.display = "block";
                    } else {
                        ele.listEmpty.style.display = "none";
                        drawFriendList(detail);
                        page++;
                        if (page > res.data.list.last_page) {
                            more.innerText = '没有更多了';
                            more.onclick = null;
                        } else {
                            more.innerText = '加载更多';
                            more.onclick = () => getFriendList(type, page);
                        }
                    }
                } else openTips(res.msg);
            }
        });
    }

    // 渲染直推好友列表
    function drawFriendList(data) {
        let html = '';
        for (let i = 0; i < data.length; i++) {
            let d = data[i];
            html += '<div class="jiav">' +
                '<div class="jsaj">' +
                '<img class="toux" src="' + d.head_pic + '" alt="">' +
                '<div class="jial">' +
                '<p class="j1">' + d.nick_name + '</p>' +
                '<p class="j2">' + getTime(d.register_at, 'YYYY-MM-DD HH:mm:ss') + '</p>' +
                '</div>' +
                '</div>' +
                '<div class="sale" onclick=submits(' + JSON.stringify(d) + ')>' +
                '<div class="salen">查看' + '</div>' +
                '</div>' +
                '</div>'
        }
        ele.direct.innerHTML += html;

    }



    // 拉取间推列表
    function getFriendList2(type, page) {
        // console.log(type);
        let more = ele.mores[1];
        more.innerText = '加载中...';
        let token = getCookie("token");
        let userid = getCookie("userid");
        ajax({
            url: API_PATH.FAMILYLIST,
            method: 'GET',
            header: {
                Authorization: token
            },
            data: {
                page: page,
                invite_type: 2,
                type: type
            },
            success: res => {
                if (res.code == 1) {
                    ele.indirectNum.innerHTML = '间推(' + res.data.list.total + ')';
                    let detail = res.data.list.data
                    if (detail.length == 0) {
                        more.style.display = "none";
                        ele.listEmpty2.style.display = "block";
                    } else {
                        ele.listEmpty2.style.display = "none";
                        drawFriendList2(detail);
                        page++;
                        if (page > res.data.list.last_page) {
                            more.innerText = '没有更多了';
                            more.onclick = null;
                        } else {
                            more.innerText = '加载更多';
                            more.onclick = () => getFriendList2(type, page);
                        }
                    }
                } else openTips(res.msg);
            }
        });
    }

    // 渲染间推好友列表
    function drawFriendList2(data) {
        let html = '';
        for (let i = 0; i < data.length; i++) {
            let d = data[i];
            html += '<div class="jiav">' +
                '<div class="jsaj">' +
                '<img class="toux" src="' + d.head_pic + '" alt="">' +
                '<div class="jial">' +
                '<p class="j1">' + d.nick_name + '</p>' +
                '<p class="j2">' + getTime(d.register_at, 'YYYY-MM-DD HH:mm:ss') + '</p>' +
                '</div>' +
                '</div>' +
                '<div class="sale" onclick=submits(' + JSON.stringify(d) + ')>' +
                '<div class="salen">查看' + '</div>' +
                '</div>' +
                '</div>'
        }
        ele.indirect.innerHTML += html;

    }


    // 查看弹框信息
    window.submits = submits
    function submits(data) {
        // console.log(data);
        ele.header.src = data.head_pic//弹框头像
        ele.nickname.innerHTML = data.nick_name//弹框昵称
        if (data.is_vip == 1) {
            ele.vip.style.display = 'block'
        } else {
            ele.vip.style.display = 'none'
        }
        ele.familyLevel.innerHTML = data.family_level + '级家族'//弹框家族等级
        ele.myinfo.innerHTML = (data.server_name || "暂无") + ' | ' + (data.role_name || "暂无") //弹框区服
        ele.ID.innerHTML = data.invite_code || '暂未设置'//弹框ID
        ele.weixin.innerHTML = data.wechat || '暂未设置'//弹框微信
        ele.QQ.innerHTML = data.qq || '暂未设置'//弹框QQ
        ele.modal_message.style.display = 'block'
    }
    // 复制
    for (let i = 0; i < ele.copy.length; i++) {
        ele.copy[i].onclick = () => {
            let flag = copy(ele.IDS[i].innerText);
            if (flag) openTips("复制成功");
            else openTips("复制失败，请手动复制");
        }
    }

    function getTime(timestamp, format) {
        if (String(timestamp).length === 10) {
            timestamp = timestamp * 1000
        }
        var date = new Date(timestamp)
        var Y = date.getFullYear()
        var M = date.getMonth() + 1
        var D = date.getDate()
        var hour = date.getHours()
        var min = date.getMinutes()
        var sec = date.getSeconds()
        if (format === 'YYYY') {
            return Y // 2021
        } else if (format === 'YYYY-MM') { // 2021-07
            return Y + '-' + (M < 10 ? '0' + M : M)
        } else if (format === 'YYYY-MM-DD') { // 2021-07-12
            return Y + '-' + (M < 10 ? '0' + M : M) + '-' + (D < 10 ? '0' + D : D)
        } else if (format === 'HH:mm:ss') { // 10:20:35
            return (hour < 10 ? '0' + hour : hour) + ':' + (min < 10 ? '0' + min : min) + ':' + (sec < 10 ? '0' + sec : sec)
        } else if (format === 'YYYY-MM-DD HH:mm') { // 2021-07-12 10:20
            return Y + '-' + (M < 10 ? '0' + M : M) + '-' + (D < 10 ? '0' + D : D) + ' ' + (hour < 10 ? '0' + hour : hour) + ':' + (min < 10 ? '0' + min : min)
        } else if (format === 'YYYY-MM-DD HH:mm:ss') { // 2021-07-12 10:20:35
            return Y + '-' + (M < 10 ? '0' + M : M) + '-' + (D < 10 ? '0' + D : D) + ' ' + (hour < 10 ? '0' + hour : hour) + ':' + (min < 10 ? '0' + min : min) + ':' + (sec < 10 ? '0' + sec : sec)
        } else {
            return '--'
        }

    }

})();